package com.lanou.dllo.soccernews.detailActivty;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.dllo.soccernews.R;
import com.lanou.dllo.soccernews.adapter.ReceiverAdapter;
import com.lanou.dllo.soccernews.bean.DataResult;
import com.lanou.dllo.soccernews.bean.DetailCircle;
import com.lanou.dllo.soccernews.bean.ReciverBean;
import com.lanou.dllo.soccernews.view.MyListView;
import com.lanou.dllo.soccernews.volley.GetVolleyDatasUtil;
import com.lanou.dllo.soccernews.volley.VolleySingleton;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DetailCirclePageActivity extends AppCompatActivity {
    private TextView team_name, circle_author, circle_content, circle_last_up_users, countTv;
    private ImageView team_log, autour_log;
    private ImageView thumb1, thumb2, thumb3;
    private VolleySingleton volleySingleton;
    private String[] username;
    private String usernames;

    private ReceiverAdapter adapter;
    private MyListView listview;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_circle_page);

        Intent intent = getIntent();
        int id = intent.getIntExtra("id", 0);

        init();
        String url = "https://api.dongqiudi.com/topics/" + id;
        String reciverUrl = "https://api.dongqiudi.com/topics/replies/" + id + "?sort=up";
        addData(url);
        addDataReciver(reciverUrl);

    }

    void init() {
        team_name = (TextView) findViewById(R.id.circle_name);
        circle_author = (TextView) findViewById(R.id.circle_username);
        circle_content = (TextView) findViewById(R.id.circle_content);
        circle_last_up_users = (TextView) findViewById(R.id.circle_last_up_user);
        countTv = (TextView) findViewById(R.id.countTv);
        team_log = (ImageView) findViewById(R.id.teamLog);
        autour_log = (ImageView) findViewById(R.id.circle_log);
        thumb1 = (ImageView) findViewById(R.id.circle_thumb);
        thumb2 = (ImageView) findViewById(R.id.circle_thumb2);
        thumb3 = (ImageView) findViewById(R.id.circle_thumb3);
        volleySingleton = VolleySingleton.getInstance();
        listview = (MyListView) findViewById(R.id.detail_circle_listview);
        adapter = new ReceiverAdapter(this);
    }

    private void addData(String url) {

        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        DetailCircle detailCircle = gson.fromJson(response, DetailCircle.class);
                        if (detailCircle != null) {
                           // Log.i("lanou00", detailCircle.getContent());
                            circle_content.setText(detailCircle.getContent());
                            team_name.setText(detailCircle.getGroup().getTitle());
                            circle_author.setText(detailCircle.getAuthor().getUsername());
                            String autour_log_url = detailCircle.getAuthor().getAvatar();
                            GetVolleyDatasUtil.getNetWorkImg(autour_log_url, autour_log);
                            String team_logUrl = detailCircle.getGroup().getThumb();
                            GetVolleyDatasUtil.getNetWorkImg(team_logUrl, team_log);

                            List<DetailCircle.LastUpUsersEntity> lastUpUsersEntities = detailCircle.getLast_up_users();

                            if (lastUpUsersEntities != null && lastUpUsersEntities.size() > 0) {
                                username = new String[lastUpUsersEntities.size()];
                                for (int i = 0; i < lastUpUsersEntities.size(); i++) {

                                    username[i] = lastUpUsersEntities.get(i).getUsername();

                                }
                                usernames = username[0];
                                for (int i = 1; i < lastUpUsersEntities.size(); i++) {
                                    usernames += ",    " + username[i];
                                }
                                circle_last_up_users.setText(usernames);
                                countTv.setText("共" + lastUpUsersEntities.size() + "人赞过");

                            }

                            List<DetailCircle.AttachmentsEntity> attachmentsList = detailCircle.getAttachments();
                            switch (attachmentsList.size()) {
                                case 0:

                                    break;
                                case 1:
                                    thumb1.setVisibility(View.VISIBLE);

                                    GetVolleyDatasUtil.getNetWorkImg(attachmentsList.get(0).getThumb(), thumb1);
                                    break;
                                case 2:
                                    thumb1.setVisibility(View.VISIBLE);
                                    thumb2.setVisibility(View.VISIBLE);
                                    GetVolleyDatasUtil.getNetWorkImg(attachmentsList.get(0).getThumb(), thumb1);
                                    GetVolleyDatasUtil.getNetWorkImg(attachmentsList.get(1).getThumb(), thumb2);
                                    break;
                                case 3:
                                    thumb1.setVisibility(View.VISIBLE);
                                    thumb2.setVisibility(View.VISIBLE);
                                    thumb3.setVisibility(View.VISIBLE);

                                    GetVolleyDatasUtil.getNetWorkImg(attachmentsList.get(0).getThumb(), thumb1);
                                    GetVolleyDatasUtil.getNetWorkImg(attachmentsList.get(1).getThumb(), thumb2);
                                    GetVolleyDatasUtil.getNetWorkImg(attachmentsList.get(2).getThumb(), thumb3);
                                    break;
                            }

                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                // TODO Auto-generated method stub
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        volleySingleton.addRequest(request);
    }


    private void addDataReciver(String url) {

        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        ReciverBean reciverBean = gson.fromJson(response, ReciverBean.class);
                        if (reciverBean != null) {


                            List<ReciverBean.DataCircleResult> datas = reciverBean.getData();

                            if (datas != null && datas.size() > 0) {
                                Log.i("lanou11",datas+"");
                                adapter.addDatas(datas);
                                listview.setAdapter(adapter);

                            }


                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                // TODO Auto-generated method stub
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        volleySingleton.addRequest(request);
    }
}
